#!/bin/sh

prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
datarootdir=@datarootdir@
datadir=@datadir@
sysconfdir=@sysconfdir@

top_builddir=@top_builddir@

xsltdir=@datadir@/@PACKAGE@/xslt

[ -z "@DISABLE_REST_API@" ] && DISABLE_REST_API=0 || DISABLE_REST_API=1

PROG="${0##*/}"

print_usage()
{
    [ "$1" = 0 ] || exec >&2
    cat <<EOF
Usage: $PROG [options] [xmlfile]

$PROG 	- generate source code for control process
xmlfile - source xml-file

Valid options are:
  -h, --help        - display help
  -m, --main        - filename for main.cc. Default: main.cc
  -n, --name        - filename for *_SK files (base class implementation). Default: xmlfilename_SK
  --ask             - Use 'ask' templates. See the documentation.
  --gen-varmap	    - generate variable map. For function: long* valptr(ObjectId)
  --no-gen-statistics  - disable generate code for the account of statistics
  --alone           - Use 'alone' templates. See the documentation.
  --no-main         - Don't generate main.cc

  --make-skel name  - generate process skeleton (name.src.xml, Name.cc, Name.hh, Makefile.am, main.cc)
                      Additional arguments: --no-main, --no-makefile


EOF
    [ -n "$1" ] && exit "$1" || exit
}

#parse command line options
TEMP=`getopt -n $PROG -o h,n:,m:,a,l:,z -l help,name:,main:,no-main,topdir:,path:,alone,ask,no-ask,local:,local-include,add-cc-include,add-hh-include,make-skel:,no-makefile,gen-varmap,no-gen-statistics -- "$@"` || exit 1
eval set -- "$TEMP"

name=
uni_h=
uni_cc=
uni_main=
no_main=
xmlfile=
alone=
ask=1
localinc=0
add_cc_inc=
add_hh_inc=
xsltpath=
no_makefile=
make_skel=
make_name=

xls_h="ctl-cpp-h.xsl"
xls_c="ctl-cpp-cc.xsl"
xls_m="ctl-cpp-main.xsl"

skel_h="skel.h"
skel_c="skel.cc"
skel_m="skel-main.cc"
skel_xml="skel.src.xml"
skel_make="skel-Makefile.am"

varmap=0
genstat=1

while :; do
    case "$1" in
	-h|--help) print_usage 0
	    ;;
	-n|--name)
	    shift
		name="$1"
	    ;;
	-m|--main)
	    shift
		uni_main="$1".cc
	    ;;
	--no-main)
		no_main=1
	    ;;
	--topdir)
		shift
		top_builddir="$1"
		;;
	--alone)
		xls_h="ctl-cpp-h-alone.xsl"
		xls_c="ctl-cpp-cc-alone.xsl"
		xls_m="ctl-cpp-main-alone.xsl"
		;;

	--local-include)
		localinc=1
		;;

	--add-cc-include)
		add_cc_inc="${add_cc_inc} $1"
		;;

	--add-hh-include)
		add_hh_inc="${add_hh_inc} $1"
		;;

	--ask)
		ask=1
		;;

	--no-ask|-z)
		ask=
		;;

	--gen-varmap)
		varmap=1
	    ;;

	--no-gen-statistics)
		genstat=0
	    ;;

	-l|--local)
		shift
		xsltdir=$1
		[ -z "${xsltdir}" ] && xsltdir=.
		;;

	--xsltdir)
		shift
		xsltdir="$1"
		;;

	--path)
		shift
		xsltpath="--path $1"
		;;

    --make-skel)
	make_skel=1
	shift
	make_name="$1"
	;;

    --no-makefile)
	no_makefile=1
	;;

	--) shift; break
	;;
	*) "unrecognized option: $1"
	exit 1
	;;
    esac
    shift
done


if [ -n "$make_skel" ]; then

	[ -z "$make_name" ] && print_usage -1

	cp ${xsltdir}/$skel_h ${make_name}.h && subst "s|Skel|${make_name}|g" ${make_name}.h
	cp ${xsltdir}/$skel_c ${make_name}.cc && subst "s|Skel|${make_name}|g" ${make_name}.cc
	cp ${xsltdir}/$skel_xml ${make_name}.src.xml && subst "s|Skel|${make_name}|g" ${make_name}.src.xml
	[ -z "$no_main" ] && cp ${xsltdir}/$skel_m ${make_name}-main.cc && subst "s|Skel|${make_name}|g" ${make_name}-main.cc
	[ -z "$no_makefile" ] && cp ${xsltdir}/$skel_make Makefile.am && subst "s|Skel|${make_name}|g" Makefile.am

	exit 0
fi


xmlfile="$1"

[ -n "$xmlfile" ] || print_usage

if [ -z "$name" ]; then
	name=$( basename $( basename $xmlfile .xml ) .src )
fi

[ -z "${name}" ] && print_usage 0

uni_h="${name}_SK.h"
uni_cc="${name}_SK.cc"

if [ -z "$uni_main" ]; then
	uni_main="${name}-main.cc"
fi

if [ -n "$ask" ]; then
	xls_c=$(basename $xls_c .xsl)-ask.xsl
fi

fname=$( basename $xmlfile )

PARAMS=$( echo \
		--stringparam SK_H_FILENAME "${uni_h}" \
		--stringparam XML_FILENAME "${fname}" \
		--stringparam CNAME "${name}" \
		--stringparam LOCALINC "${localinc}" \
		--stringparam VARMAP "${varmap}" \
		--stringparam STAT "${genstat}" \
		--stringparam DISABLE_REST_API "${DISABLE_REST_API}" \
		$xsltpath \
		)
#		--stringparam ADD_CC_INC "${add_cc_inc}" \
#		--stringparam ADD_HH_INC "${add_hh_inc}" \

# generate xxx_SK class
xsltproc ${PARAMS} ${xsltdir}/${xls_h} $xmlfile > $uni_h || rm -f $uni_h
xsltproc ${PARAMS} ${xsltdir}/${xls_c} $xmlfile > $uni_cc || rm -f $uni_cc
[ -n "$no_main" ] || xsltproc ${PARAMS} ${xsltdir}/${xls_m} $xmlfile > $uni_main || rm -f $uni_main
